package com.system.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.am.annotation.Permission;
import com.am.base.BaseDao;
import com.system.uitls.CommonUtil;
import com.system.uitls.IdUtils;

public class RoleDao extends BaseDao
{
  public List<Map<String, Object>> getRoleList()
  {
    String sql = "SELECT id,name FROM sys_role";
    return getSimpleJdbc().queryForList(sql, new Object[0]);
  }

  public Map<String, String> delete(String id)
  {
    Map result = new HashMap();
    List sqlList = new ArrayList();
    List params = new ArrayList();
    try
    {
      sqlList.add("DELETE FROM sys_role WHERE id=?");
      params.add(new Object[] { id });

      sqlList.add("DELETE FROM sys_role_privilege WHERE role_id=?");
      params.add(new Object[] { id });

      sqlList.add("DELETE FROM sys_user_role WHERE role_id=?");
      params.add(new Object[] { id });

      batchInTransaction(sqlList, params);

      result.put("type", "success");
      result.put("value", "删除成功");
    }
    catch (Exception ex) {
      result.put("type", "fail");
      result.put("value", "删除失败");
    }

    return result;
  }

  public List<Map<String, Object>> getPrivilegeList(String roleId)
  {
    String sql = "SELECT id,pId,name FROM sys_privilege";
    List<Map<String, Object>> list = getSimpleJdbc().queryForList(sql, new Object[0]);

    sql = "SELECT privilege_id FROM sys_role_privilege WHERE role_id=?";
    List hasPrivilege = getJdbc().queryForList(sql, new Object[] { roleId }, String.class);

    for (Map map : list)
    {
      if (!hasPrivilege.contains(CommonUtil.nullToEmptyString(map.get("id"))))
        continue;
      map.put("checked", "true");
    }

    return list;
  }

  @Permission("role_edit,role_save")
  public Map<String, String> update(Map<String, Object> map)
  {
    String id = CommonUtil.nullToEmptyString(map.get("id"));
    String name = CommonUtil.nullToEmptyString(map.get("name"));
    String create_user = CommonUtil.nullToEmptyString(map.get("create_user"));
    String notes = CommonUtil.nullToEmptyString(map.get("notes"));
    Map result = new HashMap();
    String sql = "";

    if ("".equals(id))
      try
      {
        id = IdUtils.getSequence(getJdbc(), "sys_role_id");
        String create_time = CommonUtil.getCurDate();
        sql = "INSERT INTO sys_role(id,name,notes,create_time,modify_time,create_user) VALUES(?,?,?,?,?,?)";
        getSimpleJdbc().update(sql, new Object[] { id, name, notes, create_time, create_time, create_user });
        logger.info("RoleDao[update]-->sql:" + sql);
        result.put("type", "success");
        result.put("value", "保存成功");
      }
      catch (Exception ex) {
        logger.error("RoleDao[update]-->sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "保存失败");
      }
    else {
      try
      {
        sql = "update sys_role set name=?,notes=?,modify_time=? where id=?";
        getSimpleJdbc().update(sql, new Object[] { name, notes, CommonUtil.getCurDate(), id });
        logger.info("RoleDao[update]-->sql:" + sql);
        result.put("type", "success");
        result.put("value", "修改成功");
      }
      catch (Exception ex) {
        logger.error("RoleDao[update]-->sql:" + sql + "\n" + ex.getMessage());
        result.put("type", "fail");
        result.put("value", "修改失败");
      }
    }
    return result;
  }

  public Map<String, String> setPrivilege(String[] privilegeIds, String roleId)
  {
    Map result = new HashMap();
    List params = new ArrayList();
    List sqlList = new ArrayList();
    try
    {
      sqlList.add("DELETE FROM sys_role_privilege WHERE role_id=?");
      params.add(new Object[] { roleId });

      for (int i = 0; (privilegeIds != null) && (i < privilegeIds.length); i++)
      {
        if ("".equals(privilegeIds[i].trim()))
          continue;
        sqlList.add("INSERT INTO sys_role_privilege(id,role_id,privilege_id) VALUES(?,?,?)");
        params.add(new Object[] { IdUtils.getSequence(getJdbc(), "sys_role_privilege_id"), roleId, privilegeIds[i] });
      }

      batchInTransaction(sqlList, params);
      result.put("type", "success");
      result.put("value", "设置成功");
    } catch (Exception ex) {
      result.put("type", "fail");
      result.put("value", "设置失败");
    }

    return result;
  }
}